(function($) {
	/***************************************************************************
	 * JQuery 明细视图插件12.0
	 * 
	 * Author: 姜志光 Date: 2014年1月8日
	 **************************************************************************/

	DetailView = function(rootElement, userSettings) {

		var instance = this;

		/***********************************************************************
		 * 数据来源表单
		 **********************************************************************/
		this.fromForm = userSettings.fromForm;

		/***********************************************************************
		 * 视图主键配置
		 **********************************************************************/
		this.pfields = userSettings.pfields;

		this.viewConfig	=	userSettings;

		/***********************************************************************
		 * 视图当前选中行
		 **********************************************************************/
		this.selectRow;
		
		this.viewOperate	=	null;
		
		/***********************************************************************
		 * 视图初始化
		 **********************************************************************/
		this.init = function() {
			
			/*******************************************************************
			 * 网格初始化
			 ******************************************************************/
			var url	=	"";
			if(undefined != instance.viewConfig.vv_flow && "" != instance.viewConfig.vv_flow){
				url	=	application+'/browse/view/showViewFlowDatas.action?vv_guid='+instance.viewConfig.vv_guid;
			}else{
				url	=	application+'/browse/view/showViewDatas.action?vv_guid='+instance.viewConfig.vv_guid;
			}
			$('#gridTable').datagrid({
				width:600,
				height:350,
				singleSelect:true,
				fit: true,
				nowrap: false,
				striped: true,
				collapsible:true,
				onBeforeLoad:	function(param){
					if("undefined" != typeof(onBeforeSend)){
						onBeforeSend(param);
					}
				},
				url:url,
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				columns:[
					instance.viewConfig.listXML
				],
				pagination:true,
				rownumbers:true,
				onDblClickRow:function(rowIndex, rowData){
					dbClick(rowIndex,rowData);
				},
				onClickRow:function(rowIndex,rowData){
					instance.selectRow	=	rowData;
					instance.viewOperate.refreshOperates();
				},
				onLoadError: function(){
					alert("数据加载错误");
				},
				onLoadSuccess :function(data){
					if(undefined != data && undefined != data.message){
						alert(data.message);
					}
				}
			});
			
			instance.viewOperate	=	new ViewOperate(instance,instance.viewConfig.operateConfig, $("#operateDiv"));
			/*******************************************************************
			 * 视图操作初始化
			 ******************************************************************/
			instance.viewOperate.operateDraw();
			
			instance.onAdd();
			var searchFormInputs	=	$("#searchFormInputs");
			if(searchFormInputs.length == 0){
				return;
			}
			
			var height	=	$("#searchFormInputs").height();
			$('#contentLayout').layout('panel', 'north').panel('resize',{height:height+58});
			$('#contentLayout').layout('resize');
		};
		
		/***********************************************************************
		 * 数据添加操作
		 **********************************************************************/
		this.onAdd = function() {
			if ('' != fromForm) {
				$("#groupIFrame").attr("src",
						application + '/goForm?vf_guid=' + fromForm);
				$('#cc').layout('panel', 'east').panel({
					title : "数据添加"
				});
			}
		};

		/***********************************************************************
		 * 数据编辑操作
		 **********************************************************************/
		this.onEdit = function() {
			if ($("#detailForm").length == 0) {
				alert("未配置数据来源表单");
				return false;
			}
			var row = $("#gridTable").datagrid("getSelected");
			if (undefined == row) {
				alert("未选中行");
				return;
			}
			$("#detailDiv").html("");
			for ( var x = 0; x < pfields.length; x++) {
				$("#detailDiv").append(
						"<input type='hidden' name='viewPValues." + pfields[x]
								+ "' value='" + row[pfields[x]] + "'/>");
			}
			$('#cc').layout('panel', 'east').panel({
				title : "数据编辑"
			});
			$("#detailForm").submit();
		};

		/***********************************************************************
		 * 数据删除操作
		 **********************************************************************/
		this.onDelete = function() {
			var row = $("#gridTable").datagrid("getSelected");
			if (undefined == row) {
				alert("未选中行");
				return;
			}

			if (!confirm("您确定要删除该记录")) {
				return;
			}
			if ($("#detailForm").length == 0) {
				alert("未配置数据来源表单");
				return;
			}

			$("#deleteDiv").html("");
			for ( var x = 0; x < pfields.length; x++) {
				$("#deleteDiv").append(
						"<input type='hidden' name='pvalues." + pfields[x]
								+ "' value='" + row[pfields[x]] + "'/>");
			}
			$('#deleteForm').ajaxSubmit(function(data) {
				if ("成功删除表单数据" == data) {
					alert(data);
					$("#gridTable").datagrid('reload');
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 数据批量删除操作
		 **********************************************************************/
		this.onBatDelete = function() {
			var rows	=	instance.getSelectedRows();
			if (undefined == rows || rows.length == 0) {
				alert("未选中行");
				return;
			}

			if (!confirm("您确定要删除选中记录")) {
				return;
			}
			if ($("#batDeleteForm").length == 0) {
				alert("未配置数据来源表单");
				return;
			}

			var array	=	new Array();
			for(var index=0; index<rows.length; index++){
				var row	=	rows[index];
				for ( var x = 0; x < instance.pfields.length; x++) {
					var jobject	=	new Object();
					jobject[instance.pfields[x]]	=	row[instance.pfields[x]];
					array[array.length]	=	jobject;
				}
			}
			
			$("#batDeleteContents").val($.toJSON(array));
			$('#batDeleteForm').ajaxSubmit(function(data) {
				if ("成功删除记录" == data) {
					alert(data);
					$("#gridTable").datagrid('reload');
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 数据情况操作
		 **********************************************************************/
		this.onClear = function() {

		};

		/***********************************************************************
		 * 页面跳转操作
		 **********************************************************************/
		this.onForward = function() {

		};

		/***********************************************************************
		 * Excel导入操作
		 **********************************************************************/
		this.onExcelImport = function() {

		};

		/***********************************************************************
		 * Excel导出操作
		 **********************************************************************/
		this.onExcelExport = function() {

		};

		/***********************************************************************
		 * 视图刷新
		 **********************************************************************/
		this.onRefresh = function() {

		};

		this.onSearch = function() {

		};
		
		/***********************************************************************
		 * 获取视图多选行
		 **********************************************************************/
		this.getSelectedRows = function() {
			var checks = $(".dataSelectCheck");
			var alls = $('#gridTable').datagrid("getRows");
			var rows = new Array();
			for ( var x = 0; x < checks.length; x++) {
				var check = $(checks[x]);
				var checked = check.attr("checked");
				if (true == checked || "checked" == checked) {
					rows[rows.length] = alls[x];
				}
			}
			return rows;
		};
		
		/***********************************************************************
		 * 获取视图选中行
		 **********************************************************************/
		this.getSelectedRow	=	function(){
			return $('#gridTable').datagrid("getSelected");
		};
	};

	$.fn.listView = function(action) {
		var i, ilen, args, output, userSettings, $this = this.first(), instance = $this
				.data('ListView');
		if (typeof action !== 'string') {
			userSettings = action || {};
			if (instance) {
				instance.updateSettings(userSettings);
			} else {
				instance = new DetailView($this, userSettings);
				$this.data('ListView', instance);
				instance.init();
			}
			return $this;
		} else {
			args = [];
			if (arguments.length > 1) {
				for (i = 1, ilen = arguments.length; i < ilen; i++) {
					args.push(arguments[i]);
				}
			}

			if (instance) {
				if (typeof instance[action] !== 'undefined') {
					output = instance[action].apply(instance, args);
				} else {
					throw new Error('ListView do not provide action: ' + action);
				}
			}
			return output;
		}
	}
})(jQuery);

function showDataSelect(value, rec) {
	return "<input type='checkbox' class='dataSelectCheck'/>";
}

function showDataEdit(value,rec){
	return "<a href='javascript:onEditData()'><font color=red>修改</font></a>";
}
function showDataDelete(value,rec){
	return "<a href='javascript:onDeleteData()'><font color=red>删除</font></a>";
}
function showDataEditDelete(value,rec){
	return "<a href='javascript:onEditData()'>修改</a>&nbsp;<a href='javascript:onDeleteData()'>删除</a>";
}
function showDictMean(value,rec){
	return "";
}

function onEditData(){
	$("body").data("ListView").onEdit();
}

function onDeleteData(){
	$("body").data("ListView").onDelete();
}